zoukankan      html  css  js  c++  java
  • andorid Activity和Service音乐播放器

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.hanqi.testservice">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MusicPlay2">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".MusicPlay1">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <service
                android:name=".TestService1"
                android:enabled="true"
                android:exported="true" />
            <service
                android:name=".MusicService"
                android:enabled="true"
                android:exported="true"></service>
        </application>
    
    </manifest>

    activity_music_play1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.hanqi.blacklist.MusicPlay1"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="播放状态:"
            android:id="@+id/tv_1"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="播放"
                android:onClick="play_onclick"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="暂停"
                android:onClick="pause_onclick"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="停止"
                android:onClick="stop_onclick"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="退出"
                android:onClick="exit_onclick"/>
        </LinearLayout>
    
    </LinearLayout>

    MusicPlay1.java

    package com.hanqi.testservice;
    
    import android.media.MediaPlayer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    public class MusicPlay1 extends AppCompatActivity {
        TextView tv1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_music_play1);
    
            tv1 = (TextView)findViewById(R.id.tv_1);
            tv1.setText("播放状态:停止");
        }
        //媒体播放器的类
        private MediaPlayer mediaPlayer;
        public void play_onclick(View v)
        {
            if (mediaPlayer == null) {
                //调用MediaPlayer的静态方法create()
                mediaPlayer = MediaPlayer.create(this, R.raw.shinain);
            }
            mediaPlayer.start();
    
            tv1.setText("播放状态:正在播放");
        }
        public void stop_onclick(View v)
        {
            if (mediaPlayer != null)
            {
                mediaPlayer.stop();//停止
                mediaPlayer.reset();//重置
                mediaPlayer.release();//释放
                mediaPlayer = null;
            }
            tv1.setText("播放状态:停止");
        }
        public void pause_onclick(View v)
        {
            if (mediaPlayer != null&& mediaPlayer.isPlaying())
            {
                mediaPlayer.pause();
                tv1.setText("播放状态:暂停");
            }
    
        }
        public void exit_onclick(View v)
        {
            stop_onclick(v);
            //退出
            finish();
    
        }
    
    }

    MusicService.java

    package com.hanqi.testservice;
    
    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.IBinder;
    
    public class MusicService extends Service {
        public MusicService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private MediaPlayer mediaPlayer;
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            //获取意图传递的信息
            String action = intent.getStringExtra("action");
            switch (action)
            {
                case "play":
                    if (mediaPlayer == null)
                    {
                        mediaPlayer = MediaPlayer.create(this,R.raw.shinain);
                    }
                    mediaPlayer.start();
                    break;
                case "stop":
                    if (mediaPlayer != null)
                    {
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                        mediaPlayer.release();
                        mediaPlayer = null;
                    }
                    break;
                case "pause":
                    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                    }
                    break;
            }
    
            return super.onStartCommand(intent, flags, startId);
        }
    }

    MusicPlay2.java

    package com.hanqi.testservice;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;
    
    public class MusicPlay2 extends AppCompatActivity {
        TextView tv1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_music_play1);
    
            tv1 = (TextView)findViewById(R.id.tv_1);
            tv1.setText("播放状态1:停止");
        }
    
        public void play_onclick(View v)
        {
            Intent intent = new Intent(this,MusicService.class);
            intent.putExtra("action", "play");
            startService(intent);
            tv1.setText("播放状态1:正在播放...");
        }
        public void stop_onclick(View v)
        {
            Intent intent = new Intent(this,MusicService.class);
            intent.putExtra("action","stop");
            startService(intent);
            tv1.setText("播放状态1:停止");
        }
        public void pause_onclick(View v)
        {
            Intent intent = new Intent(this,MusicService.class);
            intent.putExtra("action","pause");
            startService(intent);
                tv1.setText("播放状态1:暂停");
        }
        public void exit_onclick(View v)
        {
            stop_onclick(v);
            //退出
            finish();
    
        }
    
    }
  • 相关阅读:
    redis 6379端口telnet不通的解决办法
    虚拟机centOS7 关闭防火墙后ping通 telnet不通 解决办法:disable seLinux
    oracle查询表名或字段查不到
    springcloud中feign使用的抗
    java分页使用本地线程方式
    PSS下载助手(PSX Download Helper)1.7.6.1发布
    Spring Cloud系列(四):Eureka源码解析之客户端
    Spring Cloud系列(三):Eureka源码解析之服务端
    Spring Cloud系列(二):Eureka应用详解
    Spring Cloud系列(一):微服务架构简介
  • 原文地址:https://www.cnblogs.com/cuikang/p/5426327.html
Copyright © 2011-2022 走看看