实现日语翻译酱(2)
博客专区 > Maxium_R 的博客 > 博客详情
实现日语翻译酱(2)
Maxium_R 发表于2年前
实现日语翻译酱(2)
  • 发表于 2年前
  • 阅读 22
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

摘要: 本文是按照日语翻译酱主题大概实现的效果。实现音频播放和进度。

接上一篇博客,先上效果图

输入图片说明。 点击listview的item时弹出PopupWindow

我们先看PopupWindow的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:wheel="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    android:background="@android:color/background_light">



    <TextView
        android:id="@+id/text1"
        tools:text="789456"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:maxLines="3"
        android:textAppearance="@android:style/TextAppearance.Holo.Large" />

    <TextView
        android:id="@+id/text2"
        tools:text="789456"
        android:maxLines="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.Holo"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        >


        <ImageView
            android:layout_weight="1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@android:drawable/ic_menu_share"/>
        <ImageView
            android:layout_weight="1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@android:drawable/ic_menu_camera"/>
        <!--FrameLayout-->
        <FrameLayout 
            android:layout_weight="1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:onClick="mc_play">
            <demo.gojapn.translationdemo.common.view.ProgressWheel
                android:id="@+id/progress_wheel"
                android:layout_width="80dp"
                android:layout_height="80dp"
                wheel:matProg_barColor="@color/colorAccent"
                wheel:matProg_fillRadius="false"
                wheel:matProg_linearProgress="false"
                wheel:matProg_barWidth="3dp"
                wheel:matProg_rimColor="#ededed"
                />
            <ImageView
                android:id="@+id/progress_wheel_src_in"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:src="@android:drawable/ic_media_play"
                android:scaleType="center"/>
        </FrameLayout>
    </LinearLayout>
</LinearLayout>

其中ProgressWheel项目可以查看这里>materialish-progress。 由于需要类似按钮效果,隐私折中的方法是使用Framelayout来实现按钮效果。同时初始化PopupWindow如下:

 private void initPop(){
       pop = new PopupWindow(this);
        pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setHeight(dip2px(this, 250));
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (service!= null){
                    service.stop();
                }
            }
        });
    }

其中service为我们播放音频的逻辑业务类。实现如下:

public class MediaPlayerService {

    private MediaPlayer player;

    private ProgressWheel wheel;
    private ImageView imgIn;
    private Thread thread ;
    private Handler mHandler = new Handler(){

        @Override
        public void dispatchMessage(Message msg) {
            if (wheel != null && player != null ){
                try {
                    wheel.setProgress(player.getCurrentPosition() *1.0f/ player.getDuration());
                } catch (Exception e) {
                    player.release();
                    player = null;
                    e.printStackTrace();
                }
            }
        }
    };

    public MediaPlayerService(Context c,@RawRes int id ) {
        //id为MP3 资源id,这边只是简单的实现功能,需要切换资源的话可以通过setter注入方式改变
        player  = MediaPlayer.create(c, id);
        if (player == null) {
            AssetFileDescriptor fileDesc =c. getResources().openRawResourceFd(id);
            if (fileDesc != null) {
                player = new MediaPlayer();
                try {
                     player.setDataSource(fileDesc.getFileDescriptor(), fileDesc.getStartOffset(), fileDesc.getLength());
                    fileDesc.close();
                } catch (IOException e) {
                   throw  new NullPointerException("ResId " +id +" not found");
                }
                if (player == null) {
                        throw  new NullPointerException("ResId " +id +" not found");
                }
            }
        }


        final AudioManager audioManager = (AudioManager)c. getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) == 0) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)/2 , 0);
        }

        player.setLooping(false);
        try {
            player.prepare();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                wheel.setProgress(1);
                 player.seekTo(0);
                player.pause();
                wheel.setProgress(0);
                imgIn.setImageResource(android.R.drawable.ic_media_play);
            }
        });
        final int milliseconds = 100;
        thread  =   new Thread(){ 
            @Override
            public void run(){
                while(true){ //每隔100毫秒检查状态。
                    try {
                        sleep(milliseconds);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(0);
                }
            }
        };
        thread.start();
    }

    public void setWheel(ProgressWheel wheel) {
        this.wheel = wheel;

    }

    public void playOrPause() {
        if (player == null) return;
        if (player.isPlaying()){
            player.pause();
            imgIn.setImageResource(android.R.drawable.ic_media_play);
        }else {
            imgIn.setImageResource(android.R.drawable.ic_media_pause);
            player.start();
        }
    }


    public void release() {
        player.release();
        player = null;
        thread.interrupt();

    }

    public void stop() {
        if (player.isPlaying()){
            imgIn.setImageResource(android.R.drawable.ic_media_play);
            player.pause();
        }
    }

    public void setParentView(View view) {
        wheel = (ProgressWheel) view.findViewById(R.id.progress_wheel);
        imgIn = (ImageView) view.findViewById(R.id.progress_wheel_src_in);
    }
}

剩下的就是在activity中调用,大致如下:

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    BaseEntity entity  = (BaseEntity) parent.getAdapter().getItem(position);
                    if (entity.getType() == BaseEntity.Type.TransSep) return;
                    showPopupView(entity,listView);
                }
            });
       private void showPopupView(BaseEntity entity,ListView listView) {

        View view = View.inflate(this,R.layout.pop_view,null);
        service.setParentView(view);
        TextView textView1 = (TextView) view.findViewById(R.id.text1);
        textView1.setText(entity.getContent_ch());
        textView1 = (TextView) view.findViewById(R.id.text2);
        textView1.setText("wdヲ「pqdkくぇd");

        pop.setFocusable(true);
        pop.setContentView(view);
        pop.showAtLocation(listView,Gravity.BOTTOM, 0, 0 );

    }
  public  void  mc_play(View v){
        //frameLayout的点击事件
        service.playOrPause();
    }

以上就实现了简单的播放和进度控制。

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 2
博文 4
码字总数 1939
×
Maxium_R
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: