MediaPlayer setVolume仅在maxed(1.0f,1.0f)时有效(未静音)

时间:2017-03-25 19:16:38

标签: android android-mediaplayer volume audio-player android-music-player

我正在处理一个简单的媒体播放器并遇到问题。当我尝试使用setVolume时,它只在设置为1.0,1.0时有效/有声。如果它将其设置为其他任何东西(例如1.0,0.8; 1.0,0.0; 0.5,1.0;等等),它将完全静音。我在Nexus 6(7.0)上进行测试,如果这有任何区别的话。

我已经尝试将setVolume放在几个不同的地方,想想它可能会有一些效果,但似乎没有什么能改变它的行为。我也试过取出setVolumeControlStream(AudioManager.STREAM_MUSIC);认为它弄乱了一些事情,但没有改变。

我最终的目标是让声音播放在右边,然后是左边,然后是右边,然后是左边,等等来回弹跳(特定的奇怪应用),所以我需要独立调整每侧的音量相当多。

编辑:能够在朋友的手机(运行6.0.1的Galaxy S5)上进行测试,并且它与相同的代码完美配合。想法?

谢谢!

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Toast;
import java.util.IllegalFormatException;


public class MainActivity extends Activity {

// variable declaration
private ListView mainList;
private MediaPlayer mp;
private final String[] listContent = { "Adele", "Moana", "Wanna be Balla", "Move Bitch", "Linkin Park"};
private Button playMusic;
private final int[] resID = { R.raw.adele, R.raw.moana, R.raw.balla, R.raw.ludacris, R.raw.linkinpark};
long startTime;
public volatile Integer i;
private SeekBar duration;
public Integer p;
private static String tag = "EMDR Player is";



@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Layout thingies
    duration = (SeekBar) findViewById(R.id.duration);
    playMusic = (Button) findViewById(R.id.playButton);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    i = 0;
    p = 0;

    // Initializing variables


    mainList = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContent);
    mainList.setAdapter(adapter);
    mp = new MediaPlayer();
    mp.setLooping(true);

    duration.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar duration, int progress,
                                      boolean fromUser) {
            p = progress;
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });




  //  final Integer sleeptime = 1000;
   /* t = new Thread(new Runnable() {
        @Override
        public void run(){
            while (i == 0) {
                try {
                    mp.setVolume(1.0f, 1.0f);
                    Thread.sleep(sleeptime);
                    mp.setVolume(0.0f, 0.0f);
                    Thread.sleep(sleeptime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        }
    });*/

    mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,
                                int position, long id) {
            playSong(position);


        }

    });
    mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.d(tag, "is onError()");
            switch(what){

                case MediaPlayer.MEDIA_ERROR_UNKNOWN:
                    // handle MEDIA_ERROR_UNKNOWN, optionally handle extras
                    handleExtras(extra);
                    break;

                case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                    // handle MEDIA_ERROR_SERVER_DIED, optionally handle extras
                    handleExtras(extra);
                    break;
            }

            return true;
        }
    });

    playMusic.setOnClickListener(new View.OnClickListener(){
        // creates a new onclick listener (does a thing when it's clicked) for our button "playMusic"
        @Override
        public void onClick(View v){

            if (mp.isPlaying()) {
                pauseMusic(); // runs the pauseMusic method
                i = 1;
                Toast.makeText(getApplicationContext(), "Elapsed time was " + ((startTime)/1000) + " seconds.", Toast.LENGTH_SHORT).show();
                Log.d(tag, "is Pause Clicked()");
            }
            else {
                resettobeginningMusic(); // starts the music from the beginning
                startMusic(); // runs the startMusic method
                i = 0;
                Log.d(tag, "is Start Clicked()");

                //toast();
            }
        }
    });}


public void playSong(int songIndex) {
    try{
    mp.reset();// stops any current playing song
    i = 0;
    mp = MediaPlayer.create(getApplicationContext(), resID[songIndex]);
    playMusic.setText(getString(R.string.button_pause));
    final int test = p;
    final Thread thread = new Thread(new MyRunnable(test));
    thread.start();
    Toast.makeText(getApplicationContext(), "Elapsed time was " + ((test)/1000) + " seconds.", Toast.LENGTH_SHORT).show();
    mp.start();
        Log.d(tag, "is Initially Started()");
    } catch (IllegalStateException | IllegalFormatException e){
        e.printStackTrace();
    }
}



public void pauseMusic(){

   try{
       if (mp !=null){
           mp.pause();
           startTime=mp.getCurrentPosition();
           playMusic.setText(getString(R.string.button_start));
           i = 1;
           Log.d(tag, "is Paused()");
       }
       } catch (IllegalStateException | IllegalFormatException e){
           e.printStackTrace();
       }
   }

public void startMusic(){
   try{
       if (mp != null) {
           i = 0;
           playMusic.setText(getString(R.string.button_pause));
           final int test = p;
           final Thread thread = new Thread(new MyRunnable(test));
           thread.start();
           Toast.makeText(getApplicationContext(), "Elapsed time was " + ((test) / 1000) + " seconds.", Toast.LENGTH_SHORT).show();
           mp.start();
           Log.d(tag, "is Started()");
       }
       } catch (IllegalStateException | IllegalFormatException e){
           e.printStackTrace();
       }

    }


public class MyRunnable implements Runnable {
    protected Integer mFoo;
    public MyRunnable(Integer foo) {
        mFoo = foo;
    }

    public void run() {while (i == 0) {
        try {
            mp.setVolume(1.0f, 0.0f);
            Thread.sleep(mFoo);
            mp.setVolume(0.0f, 1.0f);
            Thread.sleep(mFoo);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    }}
public void resettobeginningMusic() {
    try{
        if (mp != null) {
            mp.seekTo(0);
            Log.d(tag, "is Resetting()");
        }
    } catch (IllegalStateException | IllegalFormatException e){
        e.printStackTrace();
    }
    }


private void handleExtras(int extra){
    switch(extra){
        case MediaPlayer.MEDIA_ERROR_IO:
            // handle MEDIA_ERROR_IO
            break;
        case MediaPlayer.MEDIA_ERROR_MALFORMED:
            // handle MEDIA_ERROR_MALFORMED
            break;
        case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
            // handle MEDIA_ERROR_UNSPECIFIED
            break;
        case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
            // handle MEDIA_ERROR_TIMED_OUT
            break;

    }
}

protected void onDestroy(){
    if (mp !=null && mp.isPlaying()) {
        mp.stop();
        mp.release();
        mp = null;

    }
    super.onDestroy();
}

}

0 个答案:

没有答案