web-dev-qa-db-ja.com

Android:メディアプレーヤーアクティビティでのPrepareAsync()の正しい使用法

私は this チュートリアルに従っていますが、prepareAsyncの使用法が明確ではなく、私のコードは音声を出力しません。 mp3がオンラインであり、メディアプレーヤーの準備中にアクティビティをロックしたくないため、prepareAsync()を使用しています。 prepareAsyncの基本的な使い方を教えてください。

package com.example.simplemediaplayer.app;

import Android.media.AudioManager;
import Android.media.MediaPlayer;
import Android.support.v7.app.ActionBarActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Menu;
import Android.view.MenuItem;
import Android.widget.Toast;

import Java.io.IOException;


public class MediaPlayerActivity extends ActionBarActivity implements MediaPlayer.OnPreparedListener {

    private static final String TAG = "tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_player);

        String url = "http://www.brothershouse.narod.ru/music/pepe_link_-_guitar_vibe_113_club_mix.mp3"; // your URL here
        MediaPlayer myMediaPlayer = new MediaPlayer();
        myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            myMediaPlayer.setDataSource(url);
            myMediaPlayer.prepareAsync(); // might take long! (for buffering, etc)

        } catch (IOException e) {
            Toast.makeText(this, "mp3 not found", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

    }

    /** Called when MediaPlayer is ready */
    @Override
    public void onPrepared(MediaPlayer player) {
        player.start();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.media_player, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
9
avafab

解決策は、myMediaPlayerオブジェクトのsetOnPreparedListenerを呼び出し、prepareAsync()メソッドが終了するまで待つことです。

package com.example.simplemediaplayer.app;

import Android.media.AudioManager;
import Android.media.MediaPlayer;
import Android.support.v7.app.ActionBarActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Menu;
import Android.view.MenuItem;
import Android.widget.Toast;

import Java.io.IOException;


public class MediaPlayerActivity extends ActionBarActivity {

    private static final String TAG = "tag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_player);

        String url = "http://www.brothershouse.narod.ru/music/pepe_link_-_guitar_vibe_113_club_mix.mp3"; // your URL here
        MediaPlayer myMediaPlayer = new MediaPlayer();
        myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            myMediaPlayer.setDataSource(url);
            myMediaPlayer.prepareAsync(); // prepare async to not block main thread

        } catch (IOException e) {
            Toast.makeText(this, "mp3 not found", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

        //mp3 will be started after completion of preparing...
        myMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer player) {
                player.start();
            }

        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.media_player, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
30
avafab