音频媒体播放器停止serviec后,是不是在玩媒体播放器、音频、serviec

2023-09-07 23:53:23 作者:有本事你也喜欢我试试

大家好我工作的音频模块中我的应用程序,所以我简单的音频演示与服务创建。

Guys I am working on Audio module in my app so i have created on simple Audio demo with service.

它工作到播放 - 暂停 - 播放的背景但问题是奇怪的。

Its working up to Play - Pause - Playing in Background but issue is strange.

简单而推出的应用程序,我已经添加了两个按钮像

Simply while launch app i have added two Buttons like

播放/暂停(将改变标题按状态)

Play/Pause (Will change caption according state)

停止(停止服务)

现在,我说我的播放,暂停和放大器;停止的工作完美,但是当我尝试在后,点击播放一次音频停止它的原因在问题开始

Now as i said my play-pause & Stop are working perfectly but when i try to play again audio after the click on stop its cause the problem in Start

错误:05-01 13:37:42.671:E /启动(8096):java.lang.IllegalStateException

参考链接为Media Player的国家 参考链接在后台播放音频 Reference Link for Media Player State Reference Link for play audio in background

Audio_Activity.java

public class Audio_Activity extends Activity {

    private static final String TAG = "ServicesDemo";
    public static Button play_pause;
    public static Button buttonStop;
    Audio_Service ms;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ms = new Audio_Service();
        play_pause = (Button) findViewById(R.id.play_pause);
        buttonStop = (Button) findViewById(R.id.buttonStop);

        play_pause.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (isMyServiceRunning()) {
                        ms.play_pause();
                    } else {
                        startService(new Intent(Audio_Activity.this,
                                Audio_Service.class));
                        play_pause.setText("Pause");
                    }
                }
            });

        buttonStop.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (isMyServiceRunning()) {
                        ms.stop_audio();
                        stopService(new Intent(Audio_Activity.this,
                                Audio_Service.class));
                        play_pause.setText("Play");
                    } else {
                        stopService(new Intent(Audio_Activity.this,
                                Audio_Service.class));
                        play_pause.setText("Play");
                    }
                }

            });

    }

    public boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service: manager
            .getRunningServices(Integer.MAX_VALUE)) {
            if (Audio_Service.class.getName().equals(
                    service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}

Audio_Service.java

public class Audio_Service extends Service implements OnErrorListener,
OnCompletionListener, OnPreparedListener {

    public static MediaPlayer player = new MediaPlayer();
    Audio_Activity ac = new Audio_Activity();

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        try {
            Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG)
                .show();
        } catch (Exception e) {

            Log.e("ONdistroy", "" + e);
        }
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();

        try {
            Log.e("player_status", "" + player);
            String url = "MY_WEB_URL";
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.reset();
            player.setDataSource(url);
            player.prepareAsync();
            player.setOnPreparedListener(this);
            player.setOnErrorListener(this);
            player.setOnCompletionListener(this);

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("start", "" + e);
        }
        // player.start();
    }

    public void play_pause() {
        try {
            if (player != null) {
                if (player.isPlaying()) {
                    ac.play_pause.setText("Play");
                    player.pause();
                } else {
                    ac.play_pause.setText("Pause");
                    player.start();

                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("play_pause", "" + e);
        }
    }

    public void stop_audio() {
        try {
            if (player != null) {
                if (player.isPlaying()) {
                    ac.play_pause.setText("Play");
                    player.stop();
                    player.release();

                } else {
                    ac.play_pause.setText("Play");
                    player.release();

                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("stop_service", "" + e);
        }
    }

    public void cleanUp() {

        if (player != null) {
            player.stop();
            player = null;
            player.release();
        }

    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        try {
            Log.d("complete", "called...........");

            player.stop();
            player.release();
            player = null;
            stopService(new Intent(this, Audio_Service.class));
            ac.play_pause.setText("Play");

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("complete", "" + e);
        }

    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        // TODO Auto-generated method stub
        try {
            player.stop();
            player.release();
            player = null;

            // mp.reset();
            ac.play_pause.setText("Play");
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("onError", "" + e);
        }

        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        // TODO Auto-generated method stub
        try {
            player.start();
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("onPrepare", "" + e);
        }
    }

}

我添加了下面的事情清单文件

<uses-permission android:name="android.permission.INTERNET"/>
<service 
 android:name=".Audio_Service" 
 android:enabled="true" />

更新

我知道我可以在理想的状态下播放音频的,我必须设置复位(); 但不知道where.Because如果我设置的onDestroy; 清理() stop_audio() ONSTART()然后它再增加一个错误,那就是 NullPointerException异常

I know that i can play audio in ideal state for that i have to set reset(); but don't know where.Because if i set in onDestroy; or cleanUp() or stop_audio() or onStart() then its adding one more error and that is NullpointerException.

请你们帮帮我。这个错误我收到表格最后两天。 纠正我,如果我错了。

Please guys help me. this error i am getting form last two days. Correct me if i am wrong.

罐车把你的时间,我的问题。

Tanks for take your time on my question.

推荐答案

您应该使用startService()来运行你的服务,并使用粘合剂与它通信。这不是一个regualr对象,你需要的是Android OS要意识到它,你需要的行为,因为它期望你。

You should use startService() to run your service and communicate with it using a Binder. It is not a regualr object, you need the Android OS to be made aware of it and you need to behave as it expects you to.

http://developer.android.com/reference/android/app/ Service.html

您不应该自己创建一个实例,不应该直接调用它的方法。

You shouldn't create an instance yourself and shouldn't call its methods directly.