简体   繁体   English

播放Android Studio时片段中的VideoView没有声音

[英]VideoView in fragment has no sound when played android studio

Hei, I am new to android programming and i came upon a problem that i wasn't able solve on my own. 嘿,我是android编程的新手,遇到了一个我自己无法解决的问题。

This question isn't new here, and their is alot of good answers out there. 这个问题在这里不是新问题,那里有很多好的答案。 But none for the answers i have come upon has worked for me so far. 但是到目前为止,我所得到的答案都没有一个对我有用。 So that's why i posted this question again to find out if there is any other solution beside the ones that you can find here in stackoverflow. 因此,这就是为什么我再次发布此问题,以了解除了在stackoverflow中可以找到的解决方案之外,是否还有其他解决方案。

Here is my xml 这是我的xml

<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:orientation="vertical"
              android:background="@color/colorPrimary"
              tools:context="com.example.skolen.studentbudsjett.Fragments.EconomyFragment">

    <android.support.v7.widget.Toolbar
        android:id="@+id/myToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:minHeight="?attr/actionBarSize">

        <ImageView
            android:id="@+id/toolbar_logo"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/ic_calculator_name"/>

    </android.support.v7.widget.Toolbar>

    <TextView
        android:id="@+id/textHeader"
        android:textSize="25sp"
        android:layout_gravity="center"
        android:layout_marginTop="30sp"
        android:textColor="@color/colorAccent"
        android:text="@string/textHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/textCoachAdvice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        android:layout_marginStart="15sp"
        android:layout_marginEnd="15sp"
        android:text="@string/textCoachAdvice"
        android:textColor="@android:color/background_dark"/>

    <VideoView
        android:id="@+id/videoCoachMadde"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

Here is my code 这是我的代码

private void initViews(View view) {
        mTextAdvice = (TextView) view.findViewById(R.id.textCoachAdvice);
        mTextEconomyHeader = (TextView) view.findViewById(R.id.textHeader);
        mVideoEconomy = (VideoView) view.findViewById(R.id.videoCoachMadde);

    }

    private void initVideo() {
    mMediaController = new MediaController(getActivity());
    mMediaController.setAnchorView(mVideoEconomy);

    String video = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.sparing;
    Uri mUri = Uri.parse(video);
    mVideoEconomy.setMediaController(mMediaController);
    mVideoEconomy.setVideoURI(mUri);
    mVideoEconomy.requestFocus();
    mVideoEconomy.start();
    mVideoEconomy.setZOrderOnTop(true);

    mVideoEconomy.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            AudioManager mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
            mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        }
    });
}

Thanks in advance! 提前致谢!

Why set volume to 0... set it by calling audiomanager : 为什么将音量设置为0 ...通过调用audiomanager设置:

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

Hope this help: 希望这个帮助:

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

For Volume UP: 对于提高音量:

audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);

for Volume Down: 降低音量:

audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM